﻿using MusicPlayerPlus.Interface.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MusicPlayerPlus.PlugIn.XiamiListEdit
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window, InterfaceBase,
        //Input
        MusicPlayerPlus.Interface.Base.Action.Input.IAutoExecuteAction,
        //Output
        MusicPlayerPlus.Interface.Base.Action.Output.IAddToMenuAction
    {
        public MainWindow()
        {
            InitializeComponent();
            //list.ItemsSource = XiamiUrl;
        }

        Dictionary<string, string> xiamiUrl = new Dictionary<string, string>();

        public Dictionary<string, string> XiamiUrl
        {
            get
            {
                if (xiamiUrl == null)
                {
                    xiamiUrl = new Dictionary<string, string>();
                }
                return xiamiUrl;
            }
            set
            {
                xiamiUrl = value;
            }
        }
        /// <summary>
        /// 自动执行动作接口
        /// </summary>
        public void AutoExecute()
        {
            //Dictionary<string, string> xiamiUrl = new Dictionary<string, string>();
            //xiamiUrl.Add("虾米音乐", "http://www.xiami.com/song/play?ids=/song/playlist/id/1769382897%2C1769577231%2C1770554731%2C3500523%2C3501622%2C1769356072%2C1769356032%2C1771217681%2C1769567881%2C1771315137%2C1770082556%2C1770275176%2C1769242422%2C1769866643%2C1771194184%2C3442950%2C1770480302%2C1770691279%2C1771194180%2C1770934558%2C1770103824%2C1770239267%2C1770733013%2C3592888%2C3592887/object_name/default/object_id/0");
            //xiamiUrl.Add("常听列表", "http://www.xiami.com/song/play?ids=/song/playlist/id/12956956/type/3");

            //MusicPlayerPlus.Method.Data.Settings.SaveSettings("XiamiUrl", xiamiUrl);

            //Dictionary<string, string> xiamiUrl = MusicPlayerPlus.Method.Pubic.StaticData.Settings.GetSettings<Dictionary<string, string>>("XiamiUrl");

            //if (xiamiUrl == null)
            //{
            //    return;
            //}
            //foreach (var item in xiamiUrl)
            //{
            //    GetXiamiMusic(item.Key, item.Value);
            //}

            AddToMenu_Action("编辑虾米列表", () =>
            {
                this.Show();
                this.WindowState = System.Windows.WindowState.Normal;
            });

            ReadList();
            btn_add.IsEnabled = true;
            btn_del.IsEnabled = true;
        }

        /// <summary>
        /// 添加菜单动作接口
        /// </summary>
        public Action<string, System.Action> AddToMenu_Action { private get; set; }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            string key = txt_key.Text.Trim();
            string val = txt_val.Text.Trim();
            if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(val))
            {
                MessageBox.Show("KeyName和地址不能为空");
            }
            if (!XiamiUrl.ContainsKey(key))
            {
                XiamiUrl.Add(key, val);
                SaveList();
                ReadList();
            }
            else
            {
                MessageBox.Show("已经存在的KeyName");
            }
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("确定删除？", "确定？", MessageBoxButton.OKCancel) != MessageBoxResult.OK)
            {
                return;
            }
            string key = GetSelectItemKey();
            if (XiamiUrl.ContainsKey(key))
            {
                XiamiUrl.Remove(key);
                SaveList();
                ReadList();
            }
        }

        private string GetSelectItemKey()
        {
            if (list.SelectedItem is KeyValuePair<string, string>)
            {
                KeyValuePair<string, string> item = (KeyValuePair<string, string>)list.SelectedItem;
                return item.Key;
            }
            return null;
        }

        private void ReadList()
        {
            XiamiUrl = MusicPlayerPlus.Method.Pubic.StaticData.Settings.GetSettings<Dictionary<string, string>>("XiamiUrl");
            //foreach (var item in XiamiUrl)
            //{
            //    list.Items.Add(item);
            //}
            list.ItemsSource = XiamiUrl;
        }

        private void SaveList()
        {
            MusicPlayerPlus.Method.Pubic.StaticData.Settings.SaveSettings("XiamiUrl", XiamiUrl);
        }

        private void Window_Closing_1(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }
    }
}
